﻿// Copyright (c) 2008 Paul Schaeflein
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in 
// the Software without restriction, including without limitation the rights to use, 
// copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the 
// Software, and to permit persons to whom the Software is furnished to do so, subject 
// to the following conditions:

// The above copyright notice and this permission notice shall be included in all 
// copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE 
// OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  

using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.WebControls;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;

namespace Schaeflein.Community.WebParts
{
  public class ImageUploadEditor : EditorPart
  {

    #region Properties

    private TextBox imageUrl;
    private TextBox ImageUrl
    {
      get
      {
        EnsureChildControls();
        return imageUrl;
      }
    }

    private TextBox imageAltText;
    private TextBox ImageAltText
    {
      get
      {
        EnsureChildControls();
        return imageAltText;
      }
    }

    private TextBox imageHeight;
    private TextBox ImageHeight
    {
      get
      {
        EnsureChildControls();
        return imageHeight;
      }
    }
    
    private TextBox imageWidth;
    private TextBox ImageWidth
    {
      get
      {
        EnsureChildControls();
        return imageWidth;
      }
    }

    private Label errorMessage;
    private Label ErrorMessage
    {
      get
      {
        EnsureChildControls();
        return errorMessage;
      }
    }

#endregion
    
    public ImageUploadEditor()
    {
      Title = "Image Attributes";
    }

    public override bool ApplyChanges()
    {
      bool result = false;

      if (ValidateEntries())
      {
        ImageUpload part = (ImageUpload)WebPartToEdit;
        part.ImageUrl = ImageUrl.Text;
        part.ImageAltText = this.Page.Server.HtmlEncode(ImageAltText.Text);
        part.ImageHeight = ImageHeight.Text;
        part.ImageWidth = ImageWidth.Text;

        result = true;
      }
      else
      {
        result = false;
      }

      return result;
    }

    public override void SyncChanges()
    {
      ImageUpload part = (ImageUpload)WebPartToEdit;
      ImageUrl.Text = part.ImageUrl;
      ImageAltText.Text = part.ImageAltText;
      ImageHeight.Text = part.ImageHeight;
      ImageWidth.Text = part.ImageWidth;
      ErrorMessage.Text = "";
    }

    protected override void CreateChildControls()
    {
      // First, create the controls that will hold the properties. We will
      // need these references to apply & sync changes.
      imageUrl = Helpers.CreateTextBox("imageUrl");
      imageAltText = Helpers.CreateTextBox("alternateText");
      imageHeight = Helpers.CreateTextBox("imageHeight", 35);
      imageWidth = Helpers.CreateTextBox("imageWidth", 35);

      // now, build the UI ...

      HtmlGenericControl div = null;

      div = Helpers.CreateUserSectionHead();
      div.Controls.Add(new LiteralControl("Image Url:<br/>&nbsp;&nbsp;"));
      div.Controls.Add(imageUrl);
      Controls.Add(div);

      div = Helpers.CreateUserSectionHead();
      div.Controls.Add(new LiteralControl("Alternate text:<br/>&nbsp;&nbsp;"));
      div.Controls.Add(imageAltText);
      Controls.Add(div);

      div = Helpers.CreateUserSectionHead();
      div.Controls.Add(new LiteralControl("Image Height (optional):<br/>&nbsp;&nbsp;"));
      div.Controls.Add(imageHeight);
      Controls.Add(div);

      div = Helpers.CreateUserSectionHead();
      div.Controls.Add(new LiteralControl("Image Width (optional):<br/>&nbsp;&nbsp;"));
      div.Controls.Add(imageWidth);
      Controls.Add(div);

      errorMessage = new Label();
      errorMessage.CssClass = "ms-ToolPaneError";
      Controls.Add(errorMessage);
    }

    private bool ValidateEntries()
    {
      bool result = false;

      //
      // Image Url 
      //
      string url = ImageUrl.Text;
      if (String.IsNullOrEmpty(url))
      {
        // not required
        result = true;
      }
      else
      {
        // if they provide a url, make sure it is in the correct format
        Uri validUri = null;
        if (Uri.TryCreate(url, UriKind.RelativeOrAbsolute, out validUri))
        {
          result = true;
        }
        else
        {
          ErrorMessage.Text = "The image url is not in the correct format.";
          result = false;
        }
      }

      //
      // Image alternate text
      //
      //   no validation needed

      //
      // Image height
      //
      string height = ImageHeight.Text;
      if (String.IsNullOrEmpty(height))
      {
        // not required
        result = true;
      }
      else
      {
        // if they provide a height, make sure it is a valid integer
        int validInt = 0;
        if (Int32.TryParse(height, out validInt))
        {
          result = true;
        }
        else
        {
          ErrorMessage.Text = "The image height is not a valid number.";
          result = false;
        }
      }

      //
      // Image width
      //
      string width = ImageWidth.Text;
      if (String.IsNullOrEmpty(width))
      {
        // not required
        result = true;
      }
      else
      {
        // if they provide a width, make sure it is a valid integer
        int validInt = 0;
        if (Int32.TryParse(width, out validInt))
        {
          result = true;
        }
        else
        {
          ErrorMessage.Text = "The image width is not a valid number.";
          result = false;
        }
      }
      

      return result;
    }
  }
}
